// pages/search/search.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        primary: app.globalData.primary, // 主题色
        ossPath: app.globalData.ossPath,
        params: { // 查询参数
            ProductCategorieNum: '',
            ProductTypeId: '',
            SizeId: '',
            pageIndex: 1,
            pageSize: 6,
            SortOrder: 0,
            ChargingMode: '',
            StrFilterValue: '',
            TeamNum: '',
            keywords: ''
        },
        searchList: [], // 查询列表
        backup: false, // 回顶部

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },
    searchCancel: function() { // 取消
        wx.navigateBack({
            delta: 1
        })
    },
    search: function(e) { // 搜索
        this.data.params.keywords = e.detail;
        this.data.params.pageIndex = 1;
        this.setData({params: this.data.params, backup: false})
        this.getTemplate().then(data => {
            console.log(data)
            if (data) {
                this.setData({searchList: data})
            }
        })

    },
    getTemplate() { // 查询模板
        return new Promise(resolve => {
            let params = '';
            for (let key in this.data.params) {
                params += key + '=' + this.data.params[key] + '&'
            }
            wx.request({
                url: app.globalData.serverPath + '/TemplateCenter',
                method: 'post',
                header: { 'content-type': app.globalData.header['content-type'] },
                data: params,
                success: ({ data }) => {
                    resolve(data.Data)
                },
                fail: err => resolve(false)
            })
        })
    },
    gotoDesign: function(e) {
        app.checkedUserInfo().then(ok => {

            if (!ok) return;
            var d = e.currentTarget.dataset.d;
            wx.setStorageSync('d', d)
            wx.setStorageSync('m', 0)
            // var data = '?d=' + d + '&team=' + app.globalData.user.team + '&token=' + app.globalData.user.token
            wx.navigateTo({
                url: '../design/design',
            })
        })
    },
    goBackup: function() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        })
        this.setData({ backup: false })
    },
    upload: function() {
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFilePaths
                console.log(app.globalData.header)
                wx.uploadFile({
                    url: 'https://api.aliyin.com/api/TeamMaterial', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    header: app.globalData.header,
                    formData: {
                        'TeamNum':  app.globalData.user.team,
                        'TypeNum': 0,
                        'TypeCategoryNum': 0,
                        'IsPublic': 0,
                    },
                    success({data}) {
                        try {
                            data = JSON.parse(JSON.parse(data))
                        }catch(e) {
                            data = false
                        }
                        console.log(data)
                        //do something
                    }
                })
            },
        })
    },
    getTicket() {
        wx.request({
            url: 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi',
            success: ({data}) => {
                console.log(data)
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.data.params.pageIndex++;
        this.getTemplate().then(data => {
            if(data) {
                this.data.searchList = [].concat(this.data.searchList, data)
                this.setData({
                    searchList: this.data.searchList,
                    backup: true
                })
            }
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})